﻿// PCH2307W1.cpp: 实现文件
//

#include "stdafx.h"
#include "CardTest.h"
#include "PCH2307W1.h"
#include "afxdialogex.h"
#include "mmsystem.h"


// CPCH2307W1 对话框

IMPLEMENT_DYNAMIC(CPCH2307W1, CDialogEx)

CPCH2307W1::CPCH2307W1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_PCH2307W1, pParent)
{
	for (int i = 0; i < ART2523_DO_CHAN_COUNT; i++)
		m_pButtons[i] = new CButton;

	m_bRuning = FALSE;
}

CPCH2307W1::~CPCH2307W1()
{
}

void CPCH2307W1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Control(pDX, IDC_BUTTON_START, m_btnStart);
	DDX_Control(pDX, IDC_STATIC_TIME, m_lblTime);
}


BEGIN_MESSAGE_MAP(CPCH2307W1, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_START, &CPCH2307W1::OnBnClickedButtonStart)
END_MESSAGE_MAP()


// CPCH2307W1 消息处理程序


void CPCH2307W1::OnBnClickedRadio(UINT nID)
{
	CButton* pButton = (CButton*)GetDlgItem(nID);
	pButton->SetCheck(!pButton->GetCheck());

	for (BYTE i = 0; i < ART2523_DO_CHAN_COUNT; i++)
	{
		BYTE nStatus = (m_pButtons[i]->GetCheck() == 0) ? 0 : 1;
		m_card.SetChannelStatus(i, nStatus);
	}

	CString szErrMsg;
	DWORD dwStart = timeGetTime();
	m_card.Output(szErrMsg);
	DWORD dwPassed = timeGetTime() - dwStart;

	CString szPassed;
	szPassed.Format(L"板卡输出开关量所用时间：%u MS", dwPassed);
	m_lblTime.SetWindowText(szPassed);
}

BOOL CPCH2307W1::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	CRect rect;
	GetClientRect(&rect);

	int nWidth = 80, nHeight = 25, nSpace = 10, nSpaceV = 30;
	int nBlockWidth = nWidth + nSpace * 2;
	int nColCount = rect.Width() / nBlockWidth;
	int nRowCount = ART2523_DO_CHAN_COUNT / nColCount;
	if (ART2523_DO_CHAN_COUNT % nColCount)
		nRowCount++;

	for (int i = 0; i < nRowCount; i++)
	{
		for (int j = 0; j < nColCount; j++)
		{
			int nIndex = i * nColCount + j;
			if (nIndex >= ART2523_DO_CHAN_COUNT)
				break;

			CString szName;
			szName.Format(L"通道%d", nIndex);

			CRect rectCtrl;
			rectCtrl.left = rect.left + 20 + j * nBlockWidth;
			rectCtrl.right = rectCtrl.left + nWidth;
			rectCtrl.top = rect.top + 100 + (nHeight + nSpaceV) * i;
			rectCtrl.bottom = rectCtrl.top + nHeight;

			m_pButtons[nIndex]->Create(szName, WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, rectCtrl, this, nIndex + 1);
		}
	}

	m_edit.SetWindowText(L"0X300");
	m_btnStart.SetFocus();

	return FALSE;  // return TRUE unless you set the focus to a control
	// 异常:  OCX 属性页应返回 FALSE
}

BOOL CPCH2307W1::DestroyWindow()
{
	CString szErrMsg;

	m_card.Reset(szErrMsg);
	m_card.CloseCard(szErrMsg);

	for (int i = 0; i < ART2523_DO_CHAN_COUNT; i++)
	{
		delete m_pButtons[i];
	}

	return CDialogEx::DestroyWindow();
}

void CPCH2307W1::OnBnClickedButtonStart()
{
	CString szErrMsg;

	if (m_bRuning)
	{
		if (m_card.Reset(szErrMsg))
		{
			if (m_card.CloseCard(szErrMsg))
			{
				m_bRuning = FALSE;
				m_edit.EnableWindow();
				m_btnStart.SetWindowText(L"开始");
			}
		}
	}
	else
	{
		CString szBaseAddress;
		m_edit.GetWindowText(szBaseAddress);
		szBaseAddress.Trim();
		UINT32 nTempAddr = 0;
		swscanf_s(szBaseAddress, L"%X", &nTempAddr);
		WORD nBaseAddr = (WORD)nTempAddr;

		m_card.SetBaseAddress(nBaseAddr);
		if (m_card.OpenCard(szErrMsg))
		{
			m_bRuning = FALSE;
			m_edit.EnableWindow(FALSE);
			m_btnStart.SetWindowText(L"停止");
		}
	}
}
